package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.service.WorkerspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkerspaceServiceImply implements WorkerspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SetmealMapper semealMapper;
    @Autowired
    private DishMapper dishMapper;


    /**
     * 获取营业数据
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);
        map.put("status", Orders.COMPLETED);

        //统计今日营业额
        Double turnover = orderMapper.getTurnover(map);
        turnover = turnover == null ? 0.0 : turnover;

        //统计今日新增用户
        Integer newUsers = userMapper.getUserStatistics(map);
        newUsers = newUsers == null ? 0 : newUsers;

        //统计今日有效订单数
        Integer validOrderCount = orderMapper.getOrderCount(map);
        validOrderCount = validOrderCount == null ? 0 : validOrderCount;

        //统计今日平均客单价---营业额/有效订单数
        Double unitPrice = 0.0;
        if(validOrderCount!=0){
            unitPrice = turnover.doubleValue()/validOrderCount;
        }

        //统计今日订单完成率
        Double orderCompletionRate = 0.0;
        if(validOrderCount!=0){
            map.remove("status");
            orderCompletionRate = validOrderCount.doubleValue()/orderMapper.getOrderCount(map);
        }

        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();

    }

    /**
     * 获取订单统计数据
     * @return
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        LocalDateTime beginTime = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Map map = new HashMap();
        map.put("begin", beginTime);
        map.put("end", endTime);

        //查询所有订单
        Integer orderCount = orderMapper.getOrderCount(map);
        //查询已取消订单数
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrderCount = orderMapper.getOrderCount(map);

        //查询已完成订单数
        map.remove("status");
        map.put("status",Orders.COMPLETED);
        Integer completedOrderCount = orderMapper.getOrderCount(map);

        //查询待派单订单数
        map.remove("status");
        map.put("status",Orders.CONFIRMED);
        Integer deliveredOrderCount = orderMapper.getOrderCount(map);

        //查询待接单订单数
        map.remove("status");
        map.put("status",Orders.TO_BE_CONFIRMED);
        Integer waitingOrderCount = orderMapper.getOrderCount(map);

        return OrderOverViewVO.builder()
                .allOrders(orderCount)
                .cancelledOrders(cancelledOrderCount)
                .completedOrders(completedOrderCount)
                .deliveredOrders(deliveredOrderCount)
                .waitingOrders(waitingOrderCount)
                .build();

    }

    /**
     * 获取套餐统计数据
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverview() {
        //查询已售套餐数
        Integer sold = semealMapper.getCount(1);
        //查询停售套餐数
        Integer discontinued = semealMapper.getCount(0);

        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 获取菜品统计数据
     * @return
     */
    @Override
    public DishOverViewVO getDishOverview() {
        //查询已售菜品数
        Integer sold = dishMapper.getCount(1);
        //查询停售菜品数
        Integer discontinued = dishMapper.getCount(0);

        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }
}
